/**
 * Created by wenchuan.li 2018/5/9.
 */
define(function () {

    var self,
        searchData = {
            keyword: ''
        },
        others = {
            ischecked: '0',
            inx: 0
        }
    ;

    var Index = app.Module.extend({

        init: function () {
            self = this;
            self._super({
                _limit: 5,
                _tag: 'p_yxsjzpindex', // key, 对象放进pageModule
                _sliderId: 'slider', // tabs
                _refreshAutoInit: true //自动加载刷新控件
            }, win);

        },
        toCancel: function (item) {
            var subdata = {
                orders: []
            };

            var _items;
            if (!item) {
                _items = self._getRefreshObj().data.filterKV({
                    ischecked: '1'
                })
            } else {
                _items = [item];
            }
            if (_items.length == 0) {
                return;
            }

            _items.forEach(function (v) {
                var no = v.GWBKNAME;
                if (v.details) {
                    v.details.forEach(function (p1) {
                        subdata.orders.push({
                            GWBKSEQ: p1.GWBKSEQ,
                            GWBKSEQ2: p1.GWBKSEQ2,
                            GWBKID: p1.GWBKID,
                            GWBKNAME: no
                        });
                    });
                }
            });

            app.ajax({
                xurl: "api/wx/gwat300/updateN",
                loading: true,
                errmsg: '取消转单操作异常', // 提交失败提示信息
                xdata: {
                    obj: app.jsonString(subdata)
                },
                confirm: {
                    msg: '是否执行取消转单操作？'
                },
                cb: function (result) {
                    if (result.ok) {
                        var _data = result.data.data;
                        if (_data.count1 > 0) {
                            app.alert({
                                msg: '已在购物车，不可取消单'
                            });
                        } else if (_data.count2 > 0) {
                            app.alert({
                                msg: '已转正式订单，不可取消单'
                            });
                        }
                        self._refresh();
                    }
                }
            });
        },
        toSubmit: function (item) {
            var _items;
            if (!item) {
                _items = self._getRefreshObj().data.filterKV({
                    ischecked: '1'
                })
            } else {
                _items = [item];
            }
            if (_items.length == 0) {
                return;
            }

            app.openPage({
                src: "gwcxd/qzsj-gwc.html",
                args: {
                    items: _items,
                    cb: function () {
                        self._refresh();
                    }
                }
            });
        },
        _initRefresh: function () { // 实例化刷新组件
            if (self._refreshIds.length == 0) {
                self._refreshIds = ['scrollY', 'scrollZ'];
            }
            this._super(win);
        },
        _refreshCb: function (goods) {// 刷新组件回调函授
            var _super = this._super;
            app.ajax({
                xurl: "api/wx/gwat300/getQzsjList",
                xdata: {
                    page:goods.page+1,
                    limit: goods.limit,
                    GWBKACTI: goods.type.replace('scroll', ''),
                    keyword: searchData.keyword
                },
                cb: function (result) {
                    if (result.ok) {
                        var data = result.data.data;

                        goods.data.pushArray(data.addMap({
                            status: result.xdata.GWBKACTI,
                            ischecked: '0'
                        }), goods.page == 0);

                        _super({
                            obj: goods,
                            dataLen: data.length,
                            nodata: self._refreshIndex == 0 ? '无未转单信息...' : '无已转单信息...'
                        });
                    }
                }
            });
        },
        _slide: function (i) {
            others.inx = i;
        },
        _lazy: function () {
            this._super();
        },
        _ready: function () {
            Vue.component('sjzpitem-template', {
                template: app.getByid(doc, 'sjzpitem-template'),
                props: {
                    model: Array
                },
                methods: {
                    showDetail: function (item) {
                        if (others.inx == 0) {
                            // 编辑提交
                            self.toSubmit(item);
                        } else {
                            // 已转单的 只查看
                            app.openPage({
                                src: "gwcxd/qzsj-gwc-msg.html",
                                argsKey: 'qzsjgwc_page',
                                args: {
                                    items: [item]
                                }
                            });
                        }
                    },
                    toSubmit: function (item) {
                        //item.ischecked = '1';
                        self.toSubmit(item);
                    },
                    toCancel: function (item) {
                        self.toCancel(item);
                    },
                    statueImg: function (item) {
                        if (item.status == 'Z') {
                            return 'slip';
                        } else if (item.status == 'Y') {
                            return 'nonSlip';
                        } else {
                            return '';
                        }
                    }
                }
            });

            new Vue({
                el: doc.getElementById('searchs'),
                data: {
                    md: searchData
                },
                methods: {
                    toSearch: function () {
                        self._refresh();
                    }
                }
            });

            new Vue({
                el: doc.getElementById('footdiv'),
                data: others,
                methods: {
                    toChecked: function () {
                        self._getRefreshObj().data.addMap({
                            ischecked: others.ischecked
                        }, true);
                    },
                    toSubmit: function () {
                        self.toSubmit();
                    },
                    toCancel: function () {
                        self.toCancel();
                    }
                }
            });
        }
    });

    return new Index();

});